
package test;


import anaydis.sort.Sorter;
import anaydis.sort.gui.ObservableSorter;
import data.IntegerDataSetGenerator;
import impl.*;
import provider.SorterProviderImpl;

import java.util.List;

public class PruebaSort {

    public static void main(String[]args){

        SorterProviderImpl sorterProvider = new SorterProviderImpl();
        Iterable<Sorter> sorters = sorterProvider.getAllSorters();

        int[] a = {10, 50, 100, 250, 500, 1000, 2000,3000, 4000, 5000};
        int[] b = {100, 1000, 10000, 50000};
/*
        //BUBBLE
        for(int i = 0; i<a.length; i++){
            sort(a[i], new BubbleSorter());
        }

        //SELECTION
        for(int i = 0; i<a.length; i++){
            sort(a[i], new SelectionSorter());
        }

        //INSERTION
        for(int i = 0; i<a.length; i++){
            sort(a[i], new InsertionSorter());
        }
*/
        //QUICK
        for(int i = 0; i<b.length; i++){
            sort(b[i], new QuickSorter());
        }

        //HSort
        for(int i = 0; i<b.length; i++){
            sort(b[i], new HSorter());
        }


        //ShellSort
        int[] secuencia = {1, 4, 13, 40, 121, 364, 1093, 3280, 9841};
        for(int i = 0; i<b.length; i++){
            sort(b[i], new ShellSorter(secuencia));
        }
        int[] secuencia2 = {1, 8, 23, 77, 281, 1073, 4193, 1657};                
        for(int i = 0; i<b.length; i++){
            sort(b[i], new ShellSorter(secuencia2));

        }

    }

    private static void sort(int n, AbstractSorter sort){

        IntegerDataSetGenerator generador = new IntegerDataSetGenerator();
        BenchMark benchMark = new BenchMark();
        ((ObservableSorter) sort).addSorterListener(benchMark);
        long timeStart;
        long timeEnd;
        long demora;

        List random = generador.createRandom(n);
        List ascending = generador.createRandom(n);
        List descending = generador.createRandom(n);

        System.out.println("Sort: "+sort.getType().name());
        System.out.println("N = "+n);

        timeStart = System.currentTimeMillis();
        sort.sort(generador.getComparator(), random);
        timeEnd = System.currentTimeMillis();
        demora = timeEnd - timeStart;
        System.out.println("Lista random, demora "+ demora +" ms");
        System.out.println("Swaps: "+benchMark.getSwaps()+". Comparisons: "+benchMark.getComparisons());

        benchMark = new BenchMark();
        ((ObservableSorter) sort).addSorterListener(benchMark);         
        timeStart = System.currentTimeMillis();
        sort.sort(generador.getComparator(), ascending);
        timeEnd = System.currentTimeMillis();
        demora = timeEnd - timeStart;
        System.out.println("Lista ascending, demora "+ demora +" ms");
        System.out.println("Swaps: "+benchMark.getSwaps()+". Comparisons: "+benchMark.getComparisons());

        benchMark = new BenchMark();
        ((ObservableSorter) sort).addSorterListener(benchMark);
        timeStart = System.currentTimeMillis();
        sort.sort(generador.getComparator(), descending);
        timeEnd = System.currentTimeMillis();
        demora = timeEnd - timeStart;
        System.out.println("Lista descending, demora "+ demora +" ms");
        System.out.println("Swaps: "+benchMark.getSwaps()+". Comparisons: "+benchMark.getComparisons());

        System.out.println();

      }
}
